package cn.ven.service;

import cn.ven.entity.Book;
import cn.ven.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BookServiceImpl implements BookService {

    @Autowired(required = false)
    BookMapper bookMapper;

    //查找所有的书本
    @Override
    public List<Book> findAll() {
        return bookMapper.findAll();
    }

    //根据id查找书本
    @Override
    public Book findById(Integer id) {
        return bookMapper.findById(id);
    }

    //分页查询
    @Override
    public List<Book> findByPage(Map<String,Object> map) {
        return bookMapper.findByPage(map);
    }


    //添加书本或者添加书本
    @Override
    public void addBook(Book book) {
        book.setTpublish(new Date()); //直接把当前时间添加到里面
        book.setFavnum(0);//喜欢数默认为0
        book.setRate(0);//点赞数默认为0
        book.setSalenum(0);//销售量默认为0
        bookMapper.addBook(book);
    }

    //更新书本
    @Override
    public void updateBook(Book book) {
        bookMapper.updateBook(book);
    }

    //获取总条数
    @Override
    public int getCount(Integer typeid) {
        return bookMapper.getCount(typeid);
    }

    //模糊查询
    @Override
    public List<Book> findIf(Map<String, Object> map) {
        return bookMapper.findIf(map);
    }

    //获取模糊查询总条数
    @Override
    public int getCountIf(String key) {
        return bookMapper.getCountIf(key);
    }

    //点赞
    @Override
    public void addRate(Integer id, Integer rate) {
        bookMapper.addRate(id,rate);
    }

    //查找前五的点赞商品
    @Override
    public List<Book> findRate5() {
        return bookMapper.findRate5();
    }

    //查找前五的喜欢商品
    @Override
    public List<Book> findFav5() {
        return bookMapper.findFav5();
    }

    //查找销售前五的商品
    @Override
    public List<Book> findSale5() {
        return bookMapper.findSale5();
    }

    //根据点赞数排序且可以分类按分页查询商品
    @Override
    public List<Book> findBookByPageRate(Integer typeid, Integer start, Integer num) {
        return bookMapper.findBookByPageRate(typeid,start,num);
    }

    //根据销售数排序且可以分类按分页查询商品
    @Override
    public List<Book> findBookByPageSale(Integer typeid, Integer start, Integer num) {
        return bookMapper.findBookByPageSale(typeid,start,num);
    }

    //根据喜欢数排序且可以分类按分页查询商品
    @Override
    public List<Book> findBookByPageFav(Integer typeid, Integer start, Integer num) {
        return bookMapper.findBookByPageFav(typeid, start, num);
    }
}
